package jp.example.sinya.fragment.manage;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import jp.example.sinya.fragment.manage.constants.FragmentConstants;
import jp.example.sinya.fragment.manage.utils.FragmentStack;
import jp.example.sinya.fragment.manage.utils.FragmentUtils;

/**
 * @author Sinya
 * @editor
 * @date 23/4/2018 7:16 PM
 * @describe
 */
public class MainActivity extends AppCompatActivity implements OnChangeFragmentListener {

    private FragmentStack fragmentStack;
    private int fragmentCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentStack = FragmentStack.create(getSupportFragmentManager(), R.id.fl_container);
    }

    private void clearFragment() {
        fragmentStack.clear();
        resetFragmentCount();
    }

    public void onClickPush(View view) {
        openNextFragment(FragmentUtils.getFragment(this, null, getFragmentCount()), false);
    }

    public void onClickReplace(View view) {
        replaceFragment(FragmentUtils.getFragment(this, null, FragmentConstants.FRAGMENT_OTHER));
    }

    public void onClickPop(View view) {
        decreaseFragmentCount();
        backLastFragment();
    }

    public void onClickClear(View view) {
        clearFragment();
    }

    public void onClickShowCounts(View view) {
        Toast.makeText(this, "There are" + getFragmentCount() + " fragments in stack", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void backLastFragment() {
        if (fragmentStack.hasBackStack()) {
            fragmentStack.pop();
            decreaseFragmentCount();
        } else {
            Toast.makeText(this, "This is MainFragment, no have last", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void backMainFragment() {
        clearFragment();
        openNextFragment(FragmentUtils.getFragment(this, null, getFragmentCount()), false);
    }

    @Override
    public void openNextFragment(Fragment fragment, boolean isPop) {
        if (fragment != null) {
            fragmentStack.push(fragment, null);
            increaseFragmentCount();
        }
    }

    @Override
    public void replaceFragment(Fragment fragment) {
        if (fragmentStack.hasBackStack() && fragment != null) {
            fragmentStack.replace(fragment, null);
        } else {
            Toast.makeText(this, "This is MainFragment, no replace", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public int getFragmentCount() {
        return fragmentStack.getStackSize();
    }

    @Override
    public void decreaseFragmentCount() {
        fragmentCount--;
    }

    @Override
    public void increaseFragmentCount() {
        fragmentCount++;
    }

    @Override
    public void resetFragmentCount() {
        fragmentCount = 0;
    }
}
